import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/Layout/Index.vue'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'main',
  component: Layout,
  redirect: '/op',
  meta: {
    hidden: true,
  },

  children: [{
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      isAuth: true,
      isMenu: true,
      title: '主页',
      powerCode: '',
      hidden: true
    }

  }]
},

{
  path: '/login',
  name: 'Login',
  component: () => import('../views/Login.vue'),
  meta: {
    hidden: true
  }
},
{
  path: '/user',
  component: Layout,
  redirect: '/userlist',
  meta: {
    isAuth: true,
    isMenu: true,
    title: '用户管理',
    icon: 'iconfont icon-yonghuguanli1',
    powerCode: 'YHGL',
  },
  children: [{
    path: '/userlist',
    name: 'UserList',
    component: () => import('../views/User/UserIndex.vue'),
    meta: {
      isAuth: true,
      isMenu: true,
      title: '用户列表',
      icon: 'iconfont icon-yonghuliebiao',
      powerCode: 'YH',
    }
  },
  {
    path: '/rolelist',
    name: 'RoleList',
    component: () => import('../views/Role/RoleIndex.vue'),
    meta: {
      isAuth: true,
      isMenu: true,
      title: '角色列表',
      icon: 'iconfont icon-jiaosedingyi',
      powerCode: "JS"
    }
  },
  {
    path: '/powerlist',
    name: 'PowerList',
    component: () => import('../views/Power/Power.vue'),
    meta: {
      isAuth: true,
      isMenu: true,
      title: '权限列表',
      icon: 'iconfont icon-quanxian',
      powerCode: 'QX'
    }
  }
  ]
},

{
  path: '/employees',
  component: Layout,
  redirect: '/employeeslist',
  meta: {
    isAuth: true,
    isMenu: true,
    title: '基础信息',
    icon: 'iconfont icon-yuangongguanli1',
    powerCode: 'JCXX',
  },
  children: [
    {
      path: '/employeeslist',
      name: 'EmployeesList',
      component: () => import('../views/Employees/EmployeesIndex.vue'),
      meta: {
        isAuth: true,
        isMenu: true,
        title: '员工列表',
        icon: 'iconfont icon-yuangongguanli1',
        powerCode: 'YG',
      }
    },
    {
      path: '/employeesdetail',
      name: 'EmployeesDetail',
      component: () => import('../views/Employees/EmployeesDetail.vue'),
      meta: {
        isAuth: true,
        isMenu: false,
        title: '员工详情',
        icon: 'iconfont icon-yuangongguanli1',
        powerCode: 'YG',
      }
    },
    {
      path: '/customerslist',
      name: 'customersList',
      component: () => import('../views/Customers/CustomersIndex.vue'),
      meta: {
        isAuth: true,
        isMenu: true,
        title: '客户列表',
        icon: 'iconfont icon-kechengbiao',
        powerCode: 'KF',
      }
    },
  ]
},

{
  path: '/project',
  component: Layout,
  redirect: '/projectlist',
  meta: {
    isAuth: true,
    isMenu: true,
    title: '业务管理',
    icon: 'iconfont icon-caidan',
    powerCode: 'YWGL',
  },
  children: [
    {
      path: '/projectlist',
      name: 'ProjectList',
      component: () => import('../views/Project/ProjectIndex.vue'),
      meta: {
        isAuth: true,
        isMenu: true,
        title: '项目列表',
        icon: 'iconfont icon-hetongguanli',
        powerCode: 'XM',
      },
    },
    {
      path: '/projectdetail/:project_id',
      name: 'ProjectDetail',
      component: () => import('../views/Project/ProjectDetail.vue'),
      meta: { isAuth: true, isMenu: false, title: '项目详情', icon: 'iconfont icon-hetongguanli', powerCode: 'XM' },
      props: true,
    },
    {
      path: '/projectModulelist',
      name: 'ProjectModuleList',
      component: () => import('../views/ProjectModule/ProjectModuleIndex.vue'),
      meta: { isAuth: true, isMenu: false, title: '模块列表', icon: 'iconfont icon-kechengbiao', powerCode: 'YH' }
    },
    {
      path: '/projectdemandlist',
      name: 'ProjectDemandList',
      component: () => import('../views/ProjectDemand/ProjectDemandIndex.vue'),
      meta: {
        isAuth: true,
        isMenu: false,
        title: '需求列表',
        icon: 'iconfont icon-a-pinggudingjitubiao_huaban1fuben5',
        powerCode: 'XM',
      }
    },

    {
      path: '/projectjoblist',
      name: 'ProjectJobList',
      component: () => import('../views/ProjectJob/ProjectJobIndex.vue'),
      meta: {
        isAuth: true,
        isMenu: false,
        title: '任务列表',
        icon: 'iconfont icon-kechengbiao',
        powerCode: 'XM',
      }
    },
  ]
},

{
  path: '/op',
  name: '',
  component: Layout,
  redirect: '/operatingplatform',
  meta: {
    isAuth: true,
    isMenu: true,
    title: '汇报管理',
    icon: 'iconfont icon-yonghuguanli1',
    powerCode: 'HBGL',
  },
  children: [{
    path: '/operatingplatform',
    name: 'operatingPlatform',
    component: () => import('../views/OperatingPlatform/OperatingPlatformIndex.vue'),
    meta: {
      isAuth: true,
      isMenu: true,
      title: '工作台',
      icon: 'iconfont icon-yonghuguanli1',
      powerCode: 'GZT',
    },
  }]
},



// {
//   path: '/person',
//   component: Layout,
//   redirect: '/personlist',
//   meta: {
//     isAuth: true,
//     isMenu: true,
//     title: '个人中心',
//     icon: 'iconfont icon-gerenzhongxin',
//     powerCode: 'GRZX',
//   },
//   children: [
//     {
//       path: '/personlist',
//       name: 'personList',
//       component: () => import('../views/Person/PersonIndex.vue'),
//       meta: {
//         isAuth: true,
//         isMenu: true,
//         title: '个人中心',
//         icon: 'iconfont icon-gerenzhongxin',
//         powerCode: 'GRZX',
//       }
//     },
//   ]
// },


{
  path: '/about',
  name: 'About',
  component: () => import('../views/About.vue')
},

{
  path: '/personal',
  name: 'Personal',
  component: Layout,
  redirect: '/user_profile',
  meta: {
    isAuth: true,
    isMenu: false
  },
  children: [{
    path: '/user_profile',
    name: 'UserProfile',
    component: () => import('../views/UserProfile.vue'),
    meta: {
      isAuth: true,
      isMenu: true,
      title: '主页',
      powerCode: ''
    }
  }]
},
{
  path: '*',
  name: 'all',
  redirect: '/404',
},
{
  path: '/404',
  name: '404',
  component: () => import('../views/404.vue'),

}
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})



router.beforeEach((to, from, next) => {
  if (!to.meta.isAuth) {
    next();
    return;
  }

  if (!sessionStorage.token) {
    next('/login');
    return;
  }

  if (!to.meta.powerCode) {
    next();
    return;
  }

  if (!sessionStorage.powercodes) {
    next();
    return;
  }



  const powercodes = JSON.parse(sessionStorage.powercodes)
  if (!powercodes.find(f => f.code == to.meta.powerCode)) {
    next('/404')
    return;
  }

  next();
});

export default router